package com.xy.job.admin.controller

import com.xy.job.admin.api.request.JobInstanceQueryParam
import com.xy.job.admin.api.response.PageDTO
import com.xy.job.admin.service.JobInstanceService
import com.xy.job.core.api.response.wrapperResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

@Tag(name = "JobInstanceApi")
@Validated
@RestController
@RequestMapping("/api/jobInstance")
class JobInstanceController(
    var jobInstanceService: JobInstanceService
) {

    @Operation(summary = "查询任务实例列表")
    @PostMapping("/list")
    fun listJobInstance(@Validated @RequestBody param: JobInstanceQueryParam) = wrapperResponse {
        val page = jobInstanceService.list(param)
        return@wrapperResponse PageDTO(page)
    }

    @Operation(summary = "查询任务实例详情")
    @GetMapping("/detail")
    fun getJobInstanceDetail(jobInstanceId: Long) = wrapperResponse {
        jobInstanceService.detail(jobInstanceId)
    }

    @Operation(summary = "查询任务错误信息")
    @GetMapping("/getErrorMessage")
    fun getErrorMessage(jobInstanceId: Long) = wrapperResponse {
        jobInstanceService.getErrorMessage(jobInstanceId)
    }

    @Operation(summary = "终止任务")
    @PostMapping("/kill")
    fun killJobInstance(jobInstanceId: Long) = wrapperResponse {
        jobInstanceService.kill(jobInstanceId)
    }

    @Operation(summary = "重跑任务")
    @PostMapping("/rerun")
    fun rerun(jobInstanceId: Long) = wrapperResponse {
        jobInstanceService.rerun(jobInstanceId)
    }
}
